// pagesMine/pages/spreadList/spreadList.js
import ajax from "../../../utils/index"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // tab:['海报推送','视频课堂','文章推送'],
    tab: ['海报推送', '文章推送'],
    tab_current: 0,
    material_list: [],
    video_list: [],
    article_list: [],
    page: [1, 1, 1]
  },
  /**
   * 获取海报分类
   */
  get_poster_classifyList: function () {
    let that = this
    ajax.get('/wx/appletPoster/classifyList',{
      mediaType: 5,
      type: 1
    }).then(res => {
      if (res.code == 200) {
        res.rows.map((item, index) => {
          let material_list = that.data.material_list
          let category = {}
          category.id = ''
          category.name = ''
          category.mediaType = ''
          category.list = []
          material_list[index] = category
          that.setData({
            material_list
          })
          this.get_material_list(index, item)
        })
      }
    })
  },

  /**
   * 接口 - 获取海报列表
   */
  get_material_list: function (index, item) {
    console.log(index, item)
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    ajax.get('/wx/appletPoster/posterList',{
      categoryId: item.id,
      mediaType: item.mediaType,
      token: wx.getStorageSync('token'),
      pageNum: that.data.page[index].pageNum,
      pageSize: 10
    }).then(res => {
      if (res.code == 200) {
        if (item.mediaType == 2) { //视频
          let video_list = that.data.video_list
          let category = {}
          if (categoryId == '1396997064583417856') {
            category.name = '推广'
          } else if (categoryId == '1396997089753436160') {
            category.name = '新闻'
          } else {
            category.name = '知识'
          }
          category.list = res.rows
          video_list.push(category)
          that.setData({
            video_list: video_list,
          })
          // console.log('视频',video_list)
        } else {
          let material_list = this.data.material_list

          material_list[index].id = item.id
          material_list[index].name = item.name
          material_list[index].mediaType = item.mediaType
          material_list[index].list = material_list[index].list.concat(res.rows)
          that.setData({
            material_list: material_list,
          })
          console.log('海报', that.data.material_list)
        }
      }
      setTimeout(() => {
        wx.hideLoading()
      }, 1000)
    })
  },

  /**
   * 接口 - 文章列表
   */
  get_artcle_list: function () {
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    ajax.get('/wx/AppletArticle/list',{
      sign: 0,//0:小程序文章 1：园区文章
    }).then(res => {
      if (res.code == 200) {
        that.setData({
          article_list: res.rows
        })
      }
      setTimeout(() => {
        wx.hideLoading()
      }, 1000)
    })
  },

  //事件 - tab切换
  tabChange: function (e) {
    console.log(e.currentTarget.id)
    if (this.data.tab_current == e.currentTarget.id) { //节流
      return
    }
    this.setData({
      tab_current: e.currentTarget.id,
      material_list: [],
      video_list: [],
      article_list: [],
    })
    if (e.currentTarget.id == 0) {
      //海报
      this.get_poster_classifyList()
    } else if (e.currentTarget.id == 1) {
      //文章
      this.get_artcle_list()
    } else {
      //视频
      this.get_material_list('1396997064583417856', 2) //推广
      this.get_material_list('1396997089753436160', 2) //新闻
      this.get_material_list('1396997115502268416', 2) //知识
    }
  },
  //路由 - 分享页面
  routeShareCanvas: function (e) {
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pagesMine/pages/postersShare/postersShare?id=' + item.categoryId
    })
  },
  //路由 - 视频详情
  routeDetail: function (e) {
    let type = e.currentTarget.id
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pagesMine/pages/articleDetails/articleDetails?type=' + type + '&id=' + e.currentTarget.dataset.item.id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
    this.get_poster_classifyList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})